package top.kaoshanji.ch09;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * 代码清单 ch09-01：生产者/消费者展示同步的基础知识
 * @see URLQueue
 * @see URLProducer
 * @see URLConsumer
 *
 * author kaoshanji
 * ^_^
 * create time 2024/11/19 20:14
 */
public class URLDemo {

    private static final Logger logger = LogManager.getLogger(URLDemo.class);

    public static void main(String[] args) {
        logger.info("...代码清单 ch09-01：生产者/消费者展示同步的基础知识...");

        URLQueue queue = new URLQueue();


        URLProducer p1 = new URLProducer("P1", 3, queue);
        URLProducer p2 = new URLProducer("P2", 3, queue);


        URLConsumer c1 = new URLConsumer("C1", queue);
        URLConsumer c2 = new URLConsumer("C2", queue);


        System.out.println("Starting...");
        p1.start();
        p2.start();
        c1.start();
        c2.start();


        try {
            p1.join();
            p2.join();
        } catch (InterruptedException ie) {
            System.err.println("Interrupted waiting for producers to finish");
        }


        c1.setKeepWorking(false);
        c2.setKeepWorking(false);

        try {
            c1.join();
            c2.join();
        } catch (InterruptedException ie) {
            System.err.println("Interrupted waiting for consumers to finish");
        }



        System.out.println("Done");
    }

}
